package com.jinghong.registerlogin.service.impi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.jinghong.registerlogin.ai.V4OkHttpClientTest;
import com.jinghong.registerlogin.mapper.EvaluationMapper;
import com.jinghong.registerlogin.mapper.EvaluationQuestionMapper;
import com.jinghong.registerlogin.mapper.PaperQuestionMapper;
import com.jinghong.registerlogin.mapper.QuestionMapper;
import com.jinghong.registerlogin.model.dto.EvaluationDTO;
import com.jinghong.registerlogin.model.dto.ExamDTO;
import com.jinghong.registerlogin.model.pojo.Evaluation;
import com.jinghong.registerlogin.model.pojo.EvaluationQuestion;
import com.jinghong.registerlogin.model.pojo.PaperQuestion;
import com.jinghong.registerlogin.model.pojo.Question;
import com.jinghong.registerlogin.model.vo.EvaluationVO;
import com.jinghong.registerlogin.service.EvaluationQuestionService;
import com.jinghong.registerlogin.service.EvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.print.Paper;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:EvaluationServiceImpi
 * Package:com.jinghong.registerlogin.service.impi
 * Description:
 *
 * @Author: 寻月君子
 * @CreatTime:2024/5/26 21:56
 * Version:1.0
 */
@Service
public class EvaluationServiceimpi implements EvaluationService {
    @Autowired
    private EvaluationMapper evaluationMapper;
    @Autowired
    private EvaluationQuestionMapper evaluationQuestionMapper;
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private PaperQuestionMapper paperQuestionMapper;

    @Override
    public void insert(Evaluation evaluation) {

        evaluationMapper.insert(evaluation);


    }

    @Override
    public void update(Evaluation evaluation) {
        evaluationMapper.update(evaluation);
    }

    @Override
    public Evaluation getById(int id) {
        return evaluationMapper.getById(id);
    }

    @Override
    public EvaluationVO insertAfterExam(ExamDTO examDTO) {
        List<EvaluationQuestion> evaluationQuestions = examDTO.getEvaluationQuestions();
        Evaluation evaluation = examDTO.getEvaluation();
        //插入评估,得到id
        evaluationMapper.insert(evaluation);
        //记录拿到的分数
        Integer gerScore = 0;
        for (EvaluationQuestion evaluationQuestion : evaluationQuestions) {
            //为evaluationQuestion的evaluationId赋值
            evaluationQuestion.setEvaluationId(evaluation.getId());
            //判断对错
            Question question = questionMapper.getById(evaluationQuestion.getQuestionId());
            if (question.getAnswer().equals(evaluationQuestion.getUserAnswer())){
                evaluationQuestion.setType(1);
                gerScore += question.getScore();
            }else {
                evaluationQuestion.setType(0);
            }
        }
        evaluation.setGetScore(gerScore);
        //通过试卷id得到问题ids
        List<PaperQuestion> paperQuestions = paperQuestionMapper.getByPaperId(evaluation.getPaperId());
        List<Integer> ids = new ArrayList<>();
        for (PaperQuestion paperQuestion : paperQuestions) {
            ids.add(paperQuestion.getQuestionId());
        }
        //拿到问题
        List<Question> questions = questionMapper.bulkGet(ids);

        //AI生成总结和建议
        EvaluationDTO evaluationDTO = new EvaluationDTO(evaluation, questions, evaluationQuestions);
        System.out.println(evaluationDTO);
        evaluation  = AISummmary(evaluationDTO);
        evaluationMapper.update(evaluation);
        evaluationQuestionMapper.bulkInsert(evaluationQuestions);
        return new EvaluationVO(evaluation,evaluationQuestions);
    }


    //AI生成总结和建议
    private Evaluation AISummmary(EvaluationDTO evaluationDTO) {
        String content = "你是一个导师,能根据<<<学生的答卷>>>对学生这次考试进行总结和给出建议,并将其转化为JSON数据\n" +
                "\n" +
                "答卷解读\n" +
                "学生的答卷是一个对象字符串,其中集合evaluationQuestions的每个对象中的questionId与集合questions\n" +
                "中的id关联,也就是说集合evaluationQuestions的每个对象都关联questions集合的一个对象.其中evaluationQuestions对象中的answer为学生对该题的答案,type为他的对错,0为错1为对.\n" +
                "\n" +
                "回答解读:\n" +
                "生成一个evaluation的JSON数据,其中属性除了summary和advic由你这个导师根据其答卷给出,其它属性与输入的evaluation对象属性相同.\n" +
                "\n" +
                "\n" +
                "###\n" +
                "\n" +
                "学生的答卷例子\n" +
                "EvaluationDTO(evaluation=Evaluation(id=1, userId=3, paperId=6, getScore=30, summary=, advice=, creatTime=2024-06-01T14:38:32), questions=[Question(id=18, content=猪猪侠是不是真的很帅0, type=3, optionNum=null, a=, b=, c=, d=, e=, score=5, answer=是, detail=猪猪侠是真的很帅1, userId=3, creatTime=null, updateTime=null), Question(id=19, content=猪猪侠是不是真的很帅1, type=3, optionNum=null, a=, b=, c=, d=, e=, score=5, answer=是, detail=猪猪侠是真的很帅, userId=3, creatTime=null, updateTime=null), Question(id=20, content=猪猪侠是不是真的很帅2, type=3, optionNum=null, a=, b=, c=, d=, e=, score=5, answer=是, detail=猪猪侠是真的很帅, userId=3, creatTime=null, updateTime=null), Question(id=21, content=猪猪侠是不是真的很帅3, type=3, optionNum=null, a=, b=, c=, d=, e=, score=5, answer=是, detail=猪猪侠是真的很帅1, userId=3, creatTime=null, updateTime=null), Question(id=22, content=猪猪侠是不是真的很帅4, type=3, optionNum=null, a=, b=, c=, d=, e=, score=5, answer=是, detail=猪猪侠是真的很帅1, userId=3, creatTime=null, updateTime=null), Question(id=23, content=猪猪侠是不是真的很帅5, type=3, optionNum=null, a=, b=, c=, d=, e=, score=5, answer=是, detail=猪猪侠是真的很帅1, userId=3, creatTime=null, updateTime=null)], evaluationQuestions=[EvaluationQuestion(id=1, evaluationId=1, questionId=18, userAnswer=是, type=1), EvaluationQuestion(id=2, evaluationId=1, questionId=19, userAnswer=是, type=1), EvaluationQuestion(id=3, evaluationId=1, questionId=20, userAnswer=是, type=1), EvaluationQuestion(id=4, evaluationId=1, questionId=21, userAnswer=是, type=1), EvaluationQuestion(id=5, evaluationId=1, questionId=22, userAnswer=是, type=1), EvaluationQuestion(id=6, evaluationId=1, questionId=23, userAnswer=是, type=1)])\n" +
                "\n" +
                "回答例子\n" +
                "{\n" +
                "    \"evaluation\":{\n" +
                "\"id\":1,\n" +
                "        \"userId\":3,\n" +
                "        \"paperId\":6,    \n" +
                "        \"getScore\":30    \n" +
                "       \"summary\": \"学生在这6个问题中都给出了正确的答案，显示了良好的理解能力。\",\n" +
                "       \"advice\": \"建议学生在未来的学习中继续保持这种努力，并尝试更深入地理解每个主题，以保持和提高成绩。\",\n" +
                "'creatTime\":\"2024-06-01T14:38:32\"\n" +
                "    }\n" +
                "}\n" +
                "\n" +
                "###\n" +
                "<<<\n" +
                evaluationDTO+
                "\n" +
                ">>>\n";
        String answer = V4OkHttpClientTest.testInvoke(content);
        answer = answer.substring(answer.indexOf('{'), answer.lastIndexOf('}') + 1);
        JSONObject jsonObject = JSONObject.parseObject(answer);
        String questionsJson = JSONArray.toJSONString(jsonObject.get("evaluation"));
        return JSON.parseObject(questionsJson, Evaluation.class);
    }
}
